#include "config.h"

Config::Config() :
    nmeaMsgId(0),
    nmeaRate(0),
    rtcmMsgType(0),
    rtcmRate(0),
    baseStationIsAuto(true),
    baseStationLat(0),
    baseStationLon(0),
    baseStationHeight(0),
    isMobileStation(false),
    gyroRate(0),
    accelRate(0),
    tcpIp(""),          // 初始化 TCP 配置
    tcpPort(0),
    tcpDeviceId(""),
    mqttIp(""),
    mqttPort(0),
    mqttDeviceId(""),
    mqttUsername(""),
    mqttPassword(""),
    ntripIp(""),
    ntripPort(0),
    ntripMountPoint(""),
    ntripUsername(""),
    ntripPassword("")
{
}

QString Config::enterConfigMode()
{
    return "$CFG+\r\n";
}

QString Config::exitConfigMode()
{
    return "$CFG-\r\n";
}

QString Config::saveConfig()
{
    return "$SAVE\r\n";
}

void Config::setNmeaConfig(int msgId, double rate)
{
    nmeaMsgId = msgId;
    nmeaRate = rate;
}

void Config::setRtcmConfig(int msgType, double rate)
{
    rtcmMsgType = msgType;
    rtcmRate = rate;
}

void Config::setBaseStationConfig(bool isAuto, double lat, double lon, double height)
{
    baseStationIsAuto = isAuto;
    baseStationLat = lat;
    baseStationLon = lon;
    baseStationHeight = height;
    isMobileStation = false;
}

void Config::setMobileStation()
{
    isMobileStation = true;
    baseStationIsAuto = false;
}

void Config::setGyroConfig(double rate)
{
    gyroRate = rate;
}

void Config::setAccelConfig(double rate)
{
    accelRate = rate;
}

void Config::setTcpConfig(const QString &ip, int port, const QString &deviceId)
{
    tcpIp = ip;
    tcpPort = port;
    tcpDeviceId = deviceId;
}

void Config::setMqttConfig(const QString &ip, int port, const QString &deviceId, const QString &username, const QString &password)
{
    mqttIp = ip;
    mqttPort = port;
    mqttDeviceId = deviceId;
    mqttUsername = username;
    mqttPassword = password;
}

void Config::setNtripConfig(const QString &ip, int port, const QString &mountPoint, const QString &username, const QString &password)
{
    ntripIp = ip;
    ntripPort = port;
    ntripMountPoint = mountPoint;
    ntripUsername = username;
    ntripPassword = password;
}

// 查询命令实现
QString Config::queryMode()
{
    return "$MODE,?\r\n";
}

QString Config::queryTcpConfig()
{
    return "$TCPS,?\r\n";
}

QString Config::queryMqttConfig()
{
    return "$MQTTS,?\r\n";
}

QString Config::queryGyroConfig()
{
    return "$GYRO,?\r\n";
}

QString Config::queryAccelConfig()
{
    return "$ACEL,?\r\n";
}

QString Config::queryNtripConfig()
{
    return "$NTRIP,?\r\n";
}

QList<QString> Config::generateCommands() const
{
    QList<QString> commands;

    // NMEA 配置命令
    if (nmeaRate >= 0) {  // 修改条件以支持关闭输出 (rate = 0)
        QString nmeaCmd = QString("$NMEA,%1,%2\r\n").arg(nmeaMsgId).arg(nmeaRate, 0, 'f', 1);
        commands.append(nmeaCmd);
    }

    // RTCM 配置命令
    if (rtcmRate >= 0) {  // 修改条件以支持关闭输出 (rate = 0)
        QString rtcmCmd = QString("$RTCM,%1,%2\r\n").arg(rtcmMsgType).arg(rtcmRate, 0, 'f', 1);
        commands.append(rtcmCmd);
    }

    // 基准站/移动站配置命令
    if (isMobileStation) {
        commands.append("$MOVE\r\n");  // 修改为协议中的 $MOVE
    } else if (baseStationLat != 0 || baseStationLon != 0 || baseStationHeight != 0 || baseStationIsAuto) {
        QString baseStationCmd = baseStationIsAuto ?
            "$BASE\r\n" :  // 修改为协议中的 $BASE（自动模式）
            QString("$BASE,%1,%2,%3\r\n")
                .arg(baseStationLat, 0, 'f', 6)
                .arg(baseStationLon, 0, 'f', 6)
                .arg(baseStationHeight, 0, 'f', 2);
        commands.append(baseStationCmd);
    }

    // 陀螺计配置命令
    if (gyroRate >= 0) {  // 修改条件以支持关闭输出 (rate = 0)
        QString gyroCmd = QString("$GYRO,%1\r\n").arg(gyroRate, 0, 'f', 1);
        commands.append(gyroCmd);
    }

    // 加速度计配置命令
    if (accelRate >= 0) {  // 修改条件以支持关闭输出 (rate = 0)
        QString accelCmd = QString("$ACEL,%1\r\n").arg(accelRate, 0, 'f', 1);  // 修改为 $ACEL
        commands.append(accelCmd);
    }

    // TCP 配置命令
    if (!tcpIp.isEmpty() && tcpPort > 0 && !tcpDeviceId.isEmpty()) {
        QString tcpCmd = QString("$TCPS,%1,%2,%3\r\n").arg(tcpIp).arg(tcpPort).arg(tcpDeviceId);
        commands.append(tcpCmd);
    }

    // MQTT 配置命令
    if (!mqttIp.isEmpty() && mqttPort > 0 && !mqttDeviceId.isEmpty()) {
        QString mqttCmd = QString("$MQTTS,%1,%2,%3").arg(mqttIp).arg(mqttPort).arg(mqttDeviceId);
        if (!mqttUsername.isEmpty() && !mqttPassword.isEmpty()) {
            mqttCmd += QString(",%1,%2").arg(mqttUsername).arg(mqttPassword);
        }
        mqttCmd += "\r\n";
        commands.append(mqttCmd);
    }

    // NTRIP 配置命令
    if (!ntripIp.isEmpty() && ntripPort > 0 && !ntripMountPoint.isEmpty()) {
        QString ntripCmd = QString("$NTRIP,%1,%2,%3").arg(ntripIp).arg(ntripPort).arg(ntripMountPoint);
        if (!ntripUsername.isEmpty() && !ntripPassword.isEmpty()) {
            ntripCmd += QString(",%1,%2").arg(ntripUsername).arg(ntripPassword);
        }
        ntripCmd += "\r\n";
        commands.append(ntripCmd);
    }

    return commands;
}